package org.example.model.vo;

import lombok.Data;
import org.example.model.entity.UserDO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Data
public class LoginUser implements UserDetails {

    private  UserDO userDO;

    private List<String> permissions;

    public LoginUser(UserDO userDO) {
        this.userDO = userDO;
    }
    public LoginUser(){}
    /**
     * 权限列表
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(permissions!=null && !permissions.isEmpty()){
            return permissions.stream().map(SimpleGrantedAuthority::new).toList();
        }
        return List.of();
    }


    // 密码
    @Override
    public String getPassword() {
        return userDO.getPassword();
    }


    // 用户名
    @Override
    public String getUsername() {
        return userDO.getUserName();
    }


    // 是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
